package com.abc.ytplayerdemo;

import android.util.Log;

import androidx.annotation.NonNull;

import com.pierfrancescosoffritti.androidyoutubeplayer.core.customui.DefaultPlayerUiController;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.PlayerConstants;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.YouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.options.IFramePlayerOptions;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;

import java.util.Arrays;

public class MyYouTubePlayer {

    YouTubePlayerView playerView;
    YouTubePlayer player;
    String[] qualityLevels;
    String curQuality;
    public MyYouTubePlayer(YouTubePlayerView playerView) {
        this.playerView = playerView;
    }

    private static final String TAG = "MyYouTubePlayer";
    PlayerConstants.PlayerState state;


    interface PlayerStateListener{
        void OnPlayerState(PlayerConstants.PlayerState state);
    }
    private PlayerStateListener playerStateListener;

    public void setPlayerStateListener(PlayerStateListener playerStateListener) {
        this.playerStateListener = playerStateListener;
    }

    void start(String id)
    {
        YouTubePlayerListener listener = new AbstractYouTubePlayerListener() {
            @Override
            public void onPlaybackQualityChange(@NonNull YouTubePlayer youTubePlayer, @NonNull PlayerConstants.PlaybackQuality playbackQuality) {
                super.onPlaybackQualityChange(youTubePlayer, playbackQuality);
                Log.d(TAG, "onPlaybackQualityChange: " + playbackQuality);
                curQuality = playbackQuality.toSString();
            }

            @Override
            public void onReady(@NonNull YouTubePlayer youTubePlayer) {
                player = youTubePlayer;
//                CustomPlayerUiController customPlayerUiController = new CustomPlayerUiController(MainActivity.this, customPlayerUi, youTubePlayer, ytbView);
                DefaultPlayerUiController defaultPlayerUiController = new DefaultPlayerUiController(playerView, player);
                playerView.setCustomPlayerUi(defaultPlayerUiController.getRootView());
                defaultPlayerUiController.showUi(false);
                player.loadVideo(id, 0,"auto");
            }

            @Override
            public void onVideoId(@NonNull YouTubePlayer youTubePlayer, @NonNull String videoId) {
                super.onVideoId(youTubePlayer, videoId);
                Log.d(TAG, "onVideoId: " + id);
            }

            @Override
            public void onVideoDuration(@NonNull YouTubePlayer youTubePlayer, float duration) {
                super.onVideoDuration(youTubePlayer, duration);
                Log.d(TAG, "onVideoDuration: " + duration);
            }

            @Override
            public void onCurrentSecond(@NonNull YouTubePlayer youTubePlayer, float second) {
                super.onCurrentSecond(youTubePlayer, second);
            }

            @Override
            public void onVideoLoadedFraction(@NonNull YouTubePlayer youTubePlayer, float loadedFraction) {
                super.onVideoLoadedFraction(youTubePlayer, loadedFraction);
                Log.d(TAG, "onVideoLoadedFraction: " + loadedFraction);
            }

            @Override
            public void onAvailableQualityLevels(@NonNull YouTubePlayer youTubePlayer, @NonNull String[] qualityLevels) {
                Log.d(TAG, "onAvailableQualityLevels: " + Arrays.toString(qualityLevels));
                MyYouTubePlayer.this.qualityLevels = qualityLevels;
            }

            @Override
            public void onStateChange(@NonNull YouTubePlayer youTubePlayer, @NonNull PlayerConstants.PlayerState state) {
                super.onStateChange(youTubePlayer, state);
                Log.d(TAG, "onStateChange: " + state);
                MyYouTubePlayer.this.state = state;
                //UNKNOWN, UNSTARTED, ENDED, PLAYING, PAUSED, BUFFERING, VIDEO_CUED
                switch (state){
                    case PLAYING:
                    case BUFFERING:
                    case PAUSED:
                    case ENDED:
                    case UNKNOWN:
                    case UNSTARTED:
                    case VIDEO_CUED:
                    default:
                        break;
                }
                if(playerStateListener != null){
                    playerStateListener.OnPlayerState(state);
                }
            }
        };
        IFramePlayerOptions options = new IFramePlayerOptions.Builder()
                .controls(1)
                .build();
        playerView.initialize(listener,options);
    }


    void stop()
    {
        playerView.release();
    }

    public void setPlaybackQuality(String quality)
    {
        Log.d(TAG, "setPlaybackQuality: " + quality);
        player.setPlaybackQuality(quality);
    }

    boolean isPlaying()
    {
        return state == PlayerConstants.PlayerState.PLAYING;
    }
    void pause(){
        player.pause();
    }
    void resume(){
        player.play();
    }


    public String[] getQualityLevels() {
        return qualityLevels;
    }
    public String getCurQuality() {
        return curQuality;
    }
}
